/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Readers.java
 *
 *
 */
package UI;

import Model.Reader;
import java.awt.Component;
import java.awt.Graphics;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.Icon;
import javax.swing.JOptionPane;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author Administrator
 */
public class ReaderClient extends javax.swing.JDialog {

  
     ObjectOutputStream out;
      ObjectInputStream in;
      Socket cs;
     DefaultTableModel model;
     DefaultTableModel modelTbAdd;
      int rowSelected=-1;
      ArrayList<Reader> arrReader; 
    /** Creates new form Readers */
    public ReaderClient(java.awt.Frame parent, boolean modal,ObjectOutputStream out, ObjectInputStream in) {
             super(parent, modal);
              initComponents();
      
            
           // UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
             model = (DefaultTableModel) tbReaderUpdate.getModel();
             modelTbAdd = (DefaultTableModel) tbReaderAdd.getModel();
            arrReader = new ArrayList<>();
            this.in = in;this.out=out;
            //mo luong out,in de goi nhan du lieu voi server
           
             ShowAllReader();
            
       

    }
    //************ method exit form**************
    public void cancel() {
        int option = JOptionPane.showConfirmDialog(this, "Are you sure want to exit ?","Exit",JOptionPane.OK_CANCEL_OPTION);     
        if (option == JOptionPane.OK_OPTION) {
            this.setVisible(false);
            this.dispose();
        }
    }
    
private void ShowAllReader(){
        String keyWord = txtSearchID.getText();
        if (keyWord.equals("")) {
           try {
               out.writeObject("showAllReader");       
               
                removeTableUpdate();                        
                 arrReader=(ArrayList)in.readObject();
                  
                 for (int i = 0; i < arrReader.size(); i++) {
                   Reader  reader = (Reader) arrReader.get(i);
                 model.addRow(reader.toVector());
                }

            } catch (IOException|ClassNotFoundException|NullPointerException ex) {
                System.out.println(ex);
            } 
        }
    }
  private void removeTableUpdate() {
        while (tbReaderUpdate.getRowCount() > 0) {
            model.removeRow(tbReaderUpdate.getRowCount() - 1);
        }
    }



    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        pnReader = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        tabAddEmployee = new javax.swing.JTabbedPane();
        pnUpdate = new javax.swing.JPanel();
        btnDeleteUpdate = new javax.swing.JButton();
        btnResetUpdate = new javax.swing.JButton();
        btnCancelUpdate = new javax.swing.JButton();
        jScrollPane4 = new javax.swing.JScrollPane();
        tbReaderUpdate = new javax.swing.JTable();
        btnUpdate = new javax.swing.JButton();
        lblEmployeeIDUpate = new javax.swing.JLabel();
        lblFullNameUpdate = new javax.swing.JLabel();
        lblPhoneNumberUpdate = new javax.swing.JLabel();
        lblAddressUpdate = new javax.swing.JLabel();
        lblDepartmentUpdate = new javax.swing.JLabel();
        txtReaderID = new javax.swing.JTextField();
        txtFullName = new javax.swing.JTextField();
        txtPhoneNumber = new javax.swing.JTextField();
        txtAddress = new javax.swing.JTextField();
        lblSearchID = new javax.swing.JLabel();
        txtSearchID = new javax.swing.JTextField();
        txtCareer = new javax.swing.JTextField();
        btnSearchUpdate = new javax.swing.JButton();
        pnAddReader = new javax.swing.JPanel();
        lblReaderIDAdd = new javax.swing.JLabel();
        lblFulllNameAdd = new javax.swing.JLabel();
        lblPhoneNumberAdd = new javax.swing.JLabel();
        lblAddressAdd = new javax.swing.JLabel();
        btnAdd = new javax.swing.JButton();
        btnResetAdd = new javax.swing.JButton();
        btnCancelAdd = new javax.swing.JButton();
        txtReaderIDAdd = new javax.swing.JTextField();
        txtFullNameAdd = new javax.swing.JTextField();
        txtPhoneNumberAdd = new javax.swing.JTextField();
        txtAddressAdd = new javax.swing.JTextField();
        jScrollPane3 = new javax.swing.JScrollPane();
        tbReaderAdd = new javax.swing.JTable();
        lblPosition = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        txtCareerAdd = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Employees Management");
        setResizable(false);

        pnReader.setBackground(new java.awt.Color(0, 215, 255));
        pnReader.setPreferredSize(new java.awt.Dimension(810, 700));

        jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/id_card.png"))); // NOI18N

        tabAddEmployee.setTabPlacement(javax.swing.JTabbedPane.LEFT);

        pnUpdate.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Update Or Delete Reader", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 1, 14))); // NOI18N

        btnDeleteUpdate.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/Delete.png"))); // NOI18N
        btnDeleteUpdate.setText("Delete");
        btnDeleteUpdate.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnDeleteUpdateActionPerformed(evt);
            }
        });

        btnResetUpdate.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/reset.png"))); // NOI18N
        btnResetUpdate.setText("Reset");
        btnResetUpdate.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnResetUpdateActionPerformed(evt);
            }
        });

        btnCancelUpdate.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/Exit.png"))); // NOI18N
        btnCancelUpdate.setText("Exit");
        btnCancelUpdate.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCancelUpdateActionPerformed(evt);
            }
        });

        tbReaderUpdate.setBackground(new java.awt.Color(204, 255, 255));
        tbReaderUpdate.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null}
            },
            new String [] {
                "ReaderID", "Full Name", "Phone Number", "Adress", "Career"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false, false, false, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        tbReaderUpdate.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tbReaderUpdateMouseClicked(evt);
            }
        });
        jScrollPane4.setViewportView(tbReaderUpdate);
        tbReaderUpdate.getColumnModel().getColumn(1).setPreferredWidth(150);

        btnUpdate.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/Modify.png"))); // NOI18N
        btnUpdate.setText("Update");
        btnUpdate.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnUpdateActionPerformed(evt);
            }
        });

        lblEmployeeIDUpate.setText("Reader ID :");

        lblFullNameUpdate.setText("Full Name :");

        lblPhoneNumberUpdate.setText("Phone Number :");

        lblAddressUpdate.setText("Address :");

        lblDepartmentUpdate.setText("Career");

        txtReaderID.setEnabled(false);
        txtReaderID.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                txtReaderIDMouseClicked(evt);
            }
        });
        txtReaderID.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtReaderIDActionPerformed(evt);
            }
        });
        txtReaderID.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                txtReaderIDFocusLost(evt);
            }
        });

        txtFullName.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                txtFullNameMouseClicked(evt);
            }
        });
        txtFullName.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                txtFullNameFocusLost(evt);
            }
        });

        lblSearchID.setText("Search ReaderID:");

        txtSearchID.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                txtSearchIDMouseClicked(evt);
            }
        });

        btnSearchUpdate.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/ButtonSearch.png"))); // NOI18N
        btnSearchUpdate.setText("Search");
        btnSearchUpdate.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSearchUpdateActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout pnUpdateLayout = new javax.swing.GroupLayout(pnUpdate);
        pnUpdate.setLayout(pnUpdateLayout);
        pnUpdateLayout.setHorizontalGroup(
            pnUpdateLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnUpdateLayout.createSequentialGroup()
                .addGap(95, 95, 95)
                .addGroup(pnUpdateLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(pnUpdateLayout.createSequentialGroup()
                        .addComponent(lblSearchID)
                        .addGap(27, 27, 27)
                        .addComponent(txtSearchID, javax.swing.GroupLayout.PREFERRED_SIZE, 166, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(btnSearchUpdate))
                    .addGroup(pnUpdateLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, pnUpdateLayout.createSequentialGroup()
                            .addComponent(btnUpdate)
                            .addGap(18, 18, 18)
                            .addComponent(btnDeleteUpdate)
                            .addGap(18, 18, 18)
                            .addComponent(btnResetUpdate)
                            .addGap(18, 18, 18)
                            .addComponent(btnCancelUpdate)
                            .addGap(170, 170, 170))
                        .addGroup(pnUpdateLayout.createSequentialGroup()
                            .addGroup(pnUpdateLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(lblEmployeeIDUpate)
                                .addComponent(lblFullNameUpdate)
                                .addComponent(lblPhoneNumberUpdate)
                                .addComponent(lblAddressUpdate)
                                .addComponent(lblDepartmentUpdate))
                            .addGap(28, 28, 28)
                            .addGroup(pnUpdateLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                .addComponent(txtCareer)
                                .addComponent(txtAddress)
                                .addComponent(txtPhoneNumber)
                                .addComponent(txtFullName)
                                .addComponent(txtReaderID, javax.swing.GroupLayout.DEFAULT_SIZE, 180, Short.MAX_VALUE)))
                        .addComponent(jScrollPane4, javax.swing.GroupLayout.PREFERRED_SIZE, 501, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        pnUpdateLayout.setVerticalGroup(
            pnUpdateLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnUpdateLayout.createSequentialGroup()
                .addGap(21, 21, 21)
                .addGroup(pnUpdateLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblSearchID)
                    .addComponent(txtSearchID, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnSearchUpdate))
                .addGap(18, 18, 18)
                .addComponent(jScrollPane4, javax.swing.GroupLayout.PREFERRED_SIZE, 92, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(22, 22, 22)
                .addGroup(pnUpdateLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(lblEmployeeIDUpate)
                    .addComponent(txtReaderID, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(26, 26, 26)
                .addGroup(pnUpdateLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblFullNameUpdate)
                    .addComponent(txtFullName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(28, 28, 28)
                .addGroup(pnUpdateLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(lblPhoneNumberUpdate)
                    .addComponent(txtPhoneNumber, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(30, 30, 30)
                .addGroup(pnUpdateLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblAddressUpdate)
                    .addComponent(txtAddress, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(31, 31, 31)
                .addGroup(pnUpdateLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblDepartmentUpdate)
                    .addComponent(txtCareer, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(pnUpdateLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnUpdate)
                    .addComponent(btnDeleteUpdate)
                    .addComponent(btnResetUpdate)
                    .addComponent(btnCancelUpdate))
                .addGap(41, 41, 41))
        );

        tabAddEmployee.addTab("View Reader", new javax.swing.ImageIcon(getClass().getResource("/Images/DeleteBook.png")), pnUpdate); // NOI18N

        pnAddReader.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Add A New Reader", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 1, 14))); // NOI18N
        pnAddReader.setPreferredSize(new java.awt.Dimension(690, 539));

        lblReaderIDAdd.setText("Reader ID :");

        lblFulllNameAdd.setText("Full Name :");

        lblPhoneNumberAdd.setText("Phone Number :");

        lblAddressAdd.setText("Address :");

        btnAdd.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/Add.png"))); // NOI18N
        btnAdd.setText("Add");
        btnAdd.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAddActionPerformed(evt);
            }
        });

        btnResetAdd.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/reset.png"))); // NOI18N
        btnResetAdd.setText("Reset");
        btnResetAdd.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnResetAddActionPerformed(evt);
            }
        });

        btnCancelAdd.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/Exit.png"))); // NOI18N
        btnCancelAdd.setText("Exit");
        btnCancelAdd.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCancelAddActionPerformed(evt);
            }
        });

        txtReaderIDAdd.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                txtReaderIDAddMouseClicked(evt);
            }
        });
        txtReaderIDAdd.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtReaderIDAddActionPerformed(evt);
            }
        });
        txtReaderIDAdd.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                txtReaderIDAddFocusLost(evt);
            }
        });

        txtFullNameAdd.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                txtFullNameAddMouseClicked(evt);
            }
        });
        txtFullNameAdd.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                txtFullNameAddFocusLost(evt);
            }
        });

        tbReaderAdd.setBackground(new java.awt.Color(204, 255, 255));
        tbReaderAdd.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null}
            },
            new String [] {
                "Reader ID", "Full Name", "Phone Number", "Address", "Career"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false, false, false, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        tbReaderAdd.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tbReaderAddMouseClicked(evt);
            }
        });
        jScrollPane3.setViewportView(tbReaderAdd);

        lblPosition.setText("Career");

        jLabel3.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/LogoDVT.gif"))); // NOI18N

        javax.swing.GroupLayout pnAddReaderLayout = new javax.swing.GroupLayout(pnAddReader);
        pnAddReader.setLayout(pnAddReaderLayout);
        pnAddReaderLayout.setHorizontalGroup(
            pnAddReaderLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnAddReaderLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(pnAddReaderLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, pnAddReaderLayout.createSequentialGroup()
                        .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 428, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 117, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(21, 21, 21))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, pnAddReaderLayout.createSequentialGroup()
                        .addGroup(pnAddReaderLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(lblFulllNameAdd)
                            .addComponent(lblReaderIDAdd)
                            .addComponent(lblPhoneNumberAdd)
                            .addComponent(lblAddressAdd)
                            .addComponent(lblPosition))
                        .addGroup(pnAddReaderLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(pnAddReaderLayout.createSequentialGroup()
                                .addGap(6, 6, 6)
                                .addComponent(btnAdd)
                                .addGap(30, 30, 30)
                                .addComponent(btnResetAdd, javax.swing.GroupLayout.PREFERRED_SIZE, 99, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(13, 13, 13)
                                .addComponent(btnCancelAdd, javax.swing.GroupLayout.PREFERRED_SIZE, 103, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(pnAddReaderLayout.createSequentialGroup()
                                .addGap(66, 66, 66)
                                .addGroup(pnAddReaderLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                    .addComponent(txtPhoneNumberAdd, javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(txtAddressAdd)
                                    .addComponent(txtFullNameAdd, javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(txtReaderIDAdd, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 216, Short.MAX_VALUE)
                                    .addComponent(txtCareerAdd, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 180, Short.MAX_VALUE))))
                        .addContainerGap(207, Short.MAX_VALUE))))
        );
        pnAddReaderLayout.setVerticalGroup(
            pnAddReaderLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnAddReaderLayout.createSequentialGroup()
                .addGap(74, 74, 74)
                .addGroup(pnAddReaderLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblReaderIDAdd)
                    .addComponent(txtReaderIDAdd, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(38, 38, 38)
                .addGroup(pnAddReaderLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblFulllNameAdd)
                    .addComponent(txtFullNameAdd, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(34, 34, 34)
                .addGroup(pnAddReaderLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblPhoneNumberAdd)
                    .addComponent(txtPhoneNumberAdd, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(33, 33, 33)
                .addGroup(pnAddReaderLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(lblAddressAdd)
                    .addComponent(txtAddressAdd, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(38, 38, 38)
                .addGroup(pnAddReaderLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblPosition)
                    .addComponent(txtCareerAdd, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(43, 43, 43)
                .addGroup(pnAddReaderLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnAdd)
                    .addComponent(btnCancelAdd)
                    .addComponent(btnResetAdd))
                .addGap(27, 27, 27)
                .addGroup(pnAddReaderLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(pnAddReaderLayout.createSequentialGroup()
                        .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 93, Short.MAX_VALUE)
                        .addContainerGap())
                    .addGroup(pnAddReaderLayout.createSequentialGroup()
                        .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 92, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(8, 8, 8))))
        );

        tabAddEmployee.addTab("Add Reader  ", new javax.swing.ImageIcon(getClass().getResource("/Images/AddInsert.png")), pnAddReader); // NOI18N

        jLabel2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/BannerDVT.gif"))); // NOI18N

        javax.swing.GroupLayout pnReaderLayout = new javax.swing.GroupLayout(pnReader);
        pnReader.setLayout(pnReaderLayout);
        pnReaderLayout.setHorizontalGroup(
            pnReaderLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnReaderLayout.createSequentialGroup()
                .addGroup(pnReaderLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(tabAddEmployee, javax.swing.GroupLayout.PREFERRED_SIZE, 796, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(pnReaderLayout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 672, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        pnReaderLayout.setVerticalGroup(
            pnReaderLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnReaderLayout.createSequentialGroup()
                .addGroup(pnReaderLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1)
                    .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 141, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(tabAddEmployee, javax.swing.GroupLayout.PREFERRED_SIZE, 553, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        tabAddEmployee.getAccessibleContext().setAccessibleDescription("");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(pnReader, javax.swing.GroupLayout.PREFERRED_SIZE, 795, javax.swing.GroupLayout.PREFERRED_SIZE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(pnReader, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    
    
      public boolean validateData() {
        if (txtReaderIDAdd.getText().isEmpty()) {
            JOptionPane.showMessageDialog(null, "Reader ID cannot blank");
            txtReaderIDAdd.requestFocus();
            return false;
        }
        if (txtFullNameAdd.getText().isEmpty()) {
            JOptionPane.showMessageDialog(null, "Name cannot blank");
            txtFullNameAdd.requestFocus();
            return false;
        }
        if(txtAddressAdd.getText().isEmpty())
        {
            JOptionPane.showMessageDialog(null, "Address cannot blank");
            txtAddressAdd.requestFocus();
            return false;
        }
        if(txtCareerAdd.getText().isEmpty())
        {
            JOptionPane.showMessageDialog(null, "Career cannot blank");
            txtCareerAdd.requestFocus();
            return false;
        }
        try{
        int phoneNumber = Integer.parseInt(  txtPhoneNumberAdd.getText());
        }
        catch(Exception nfe)
        {
            JOptionPane.showMessageDialog(null, "Phone number must be number digits");
            txtFullNameAdd.requestFocus();
            return false;
        }
        return true;
    }
    
    private void btnAddActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAddActionPerformed
      
        if(!validateData())
            return;
        
        
        
        
        try {                              
            Reader newReader = new Reader(
                    txtReaderIDAdd.getText(),
                    txtFullNameAdd.getText(),
                    txtPhoneNumberAdd.getText(),
                    txtAddressAdd.getText(),
                    txtCareerAdd.getText());
            out.writeObject("insertReader");
            out.writeObject(newReader);
            String   alert=(String)in.readObject();//nhan thong bao tu server              
            if(alert.equals(""))
            {
                JOptionPane.showMessageDialog(this, "Add new reader success");
                 arrReader.add(newReader); //bookId chi tim dc dung 1 quyen
                  while (tbReaderAdd.getRowCount() > 0) 
                 modelTbAdd.removeRow(tbReaderAdd.getRowCount() - 1);
                  modelTbAdd.addRow(newReader.toVector());
                  model.addRow(newReader.toVector());
              
              }
            
            else
            {
                JOptionPane.showMessageDialog(this, alert);
            }
        } catch (ClassNotFoundException | IOException ex) {
           System.out.println(ex);
        }
        
           
}//GEN-LAST:event_btnAddActionPerformed

    private void btnResetAddActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnResetAddActionPerformed
      
     txtAddressAdd.setText(""); 
     txtCareerAdd.setText("");
     txtFullNameAdd.setText("");
     txtPhoneNumberAdd.setText("");
     txtReaderIDAdd.setText("");
        
        
}//GEN-LAST:event_btnResetAddActionPerformed

    private void btnCancelAddActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCancelAddActionPerformed
        cancel();
}//GEN-LAST:event_btnCancelAddActionPerformed

    private void txtReaderIDAddMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_txtReaderIDAddMouseClicked

}//GEN-LAST:event_txtReaderIDAddMouseClicked

    private void txtReaderIDAddActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtReaderIDAddActionPerformed

}//GEN-LAST:event_txtReaderIDAddActionPerformed

    private void txtReaderIDAddFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_txtReaderIDAddFocusLost
  
}//GEN-LAST:event_txtReaderIDAddFocusLost

    private void txtFullNameAddMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_txtFullNameAddMouseClicked
       
}//GEN-LAST:event_txtFullNameAddMouseClicked

    private void txtFullNameAddFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_txtFullNameAddFocusLost
}//GEN-LAST:event_txtFullNameAddFocusLost

    private void tbReaderAddMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tbReaderAddMouseClicked
}//GEN-LAST:event_tbReaderAddMouseClicked

    private void btnDeleteUpdateActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnDeleteUpdateActionPerformed
      int option = JOptionPane.showConfirmDialog(this, "Are you sure want to delete ?","Delete",JOptionPane.OK_CANCEL_OPTION);     
        if (option == JOptionPane.CANCEL_OPTION) {
            return;
        }      
        try {
           
            if(rowSelected==-1){
                return;
            }
            out.writeObject("deleteReader"); 
            Reader reader;
            reader=arrReader.get(rowSelected);
            out.writeObject(reader);//goi doi tuong sinh vien cho sever        
            boolean   alert=(boolean)in.readObject();//nhan thong bao tu server    
            if (alert) {
               // JOptionPane.showMessageDialog(this, "Delete Success");
                model.removeRow(rowSelected);
                arrReader.remove(reader);            
            }        
            rowSelected=-1;
        }  catch (ClassNotFoundException| IOException ex) {
            System.out.println(ex);
        }  
        
        
}//GEN-LAST:event_btnDeleteUpdateActionPerformed

    private void btnResetUpdateActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnResetUpdateActionPerformed
        txtAddress.setText("");
        txtCareer.setText("");
        txtFullName.setText("");
        txtPhoneNumber.setText("");
        txtReaderID.setText("");
        txtSearchID.setText("");
}//GEN-LAST:event_btnResetUpdateActionPerformed

    private void btnCancelUpdateActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCancelUpdateActionPerformed
               cancel();
}//GEN-LAST:event_btnCancelUpdateActionPerformed

    private void tbReaderUpdateMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tbReaderUpdateMouseClicked
      rowSelected = tbReaderUpdate.getSelectedRow();    
      Reader reader=arrReader.get(rowSelected);
      txtAddress.setText(reader.getAddress());
      txtCareer.setText(reader.getCareer());
      txtFullName.setText(reader.getName());
      txtPhoneNumber.setText(reader.getPhoneNumber());
      txtReaderID.setText(reader.getReaderID());
}//GEN-LAST:event_tbReaderUpdateMouseClicked

    private void btnUpdateActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnUpdateActionPerformed

           if(rowSelected==-1){
            JOptionPane.showMessageDialog(this,"Please choice data");
            return;
        }
           Reader editReader = new Reader(
                   txtReaderID.getText(),
                   txtFullName.getText(),
                   txtPhoneNumber.getText(),
                   txtAddress.getText(),
                   txtCareer.getText());
          
            Reader oldReader=arrReader.get(rowSelected);
            if(editReader.getName().equals(oldReader.getName()) 
                    && editReader.getAddress().equals(oldReader.getAddress())
                    && editReader.getPhoneNumber().equals(oldReader.getPhoneNumber())
                    && editReader.getCareer().equals(oldReader.getCareer()))
                             return;
               try {       
            out.writeObject("editReader");         
            out.writeObject(editReader);//goi doi tuong sinh vien cho sever        
            boolean   alert=(boolean)in.readObject();//nhan thong bao tu server    
            if (alert) {
                 JOptionPane.showMessageDialog(this,"Update Reader success");    
                 arrReader.set(rowSelected, editReader) ;
                  removeTableUpdate();                                                
                 for (int i = 0; i < arrReader.size(); i++) {
                   Reader  reader = (Reader) arrReader.get(i);
                 model.addRow(reader.toVector());
                }
                 
            }
             else{
                JOptionPane.showMessageDialog(this,"Update failed");
                   }    
        }  catch (ClassNotFoundException| IOException ex) {
            System.out.println(ex);
        }  
    }//GEN-LAST:event_btnUpdateActionPerformed

    private void txtReaderIDMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_txtReaderIDMouseClicked
}//GEN-LAST:event_txtReaderIDMouseClicked

    private void txtReaderIDActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtReaderIDActionPerformed
       
}//GEN-LAST:event_txtReaderIDActionPerformed

    private void txtReaderIDFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_txtReaderIDFocusLost
       
}//GEN-LAST:event_txtReaderIDFocusLost

    private void txtFullNameMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_txtFullNameMouseClicked
    
}//GEN-LAST:event_txtFullNameMouseClicked

    private void txtFullNameFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_txtFullNameFocusLost
}//GEN-LAST:event_txtFullNameFocusLost

private void btnSearchUpdateActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSearchUpdateActionPerformed
        // TODO add your handling code here:
      try {
                out.writeObject("searchReaderID");
                out.writeObject(txtSearchID.getText());
              
                
                Reader  reader= (Reader)in.readObject();
                if (reader == null) {
                JOptionPane.showMessageDialog(this, "No data found");
            } else {
                arrReader.removeAll(arrReader);
                arrReader.add(reader); //bookId chi tim dc dung 1 quyen
                removeTableUpdate();
                model.addRow(reader.toVector());
            }                    
            } catch (ClassNotFoundException | IOException ex) {      
                System.out.println(ex);
            }
      
}//GEN-LAST:event_btnSearchUpdateActionPerformed

private void txtSearchIDMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_txtSearchIDMouseClicked
// TODO add your handling code here:
    ShowAllReader();
}//GEN-LAST:event_txtSearchIDMouseClicked
   
    /**
     * @param args the command line arguments
     */
   
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAdd;
    private javax.swing.JButton btnCancelAdd;
    private javax.swing.JButton btnCancelUpdate;
    private javax.swing.JButton btnDeleteUpdate;
    private javax.swing.JButton btnResetAdd;
    private javax.swing.JButton btnResetUpdate;
    private javax.swing.JButton btnSearchUpdate;
    private javax.swing.JButton btnUpdate;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JScrollPane jScrollPane4;
    private javax.swing.JLabel lblAddressAdd;
    private javax.swing.JLabel lblAddressUpdate;
    private javax.swing.JLabel lblDepartmentUpdate;
    private javax.swing.JLabel lblEmployeeIDUpate;
    private javax.swing.JLabel lblFullNameUpdate;
    private javax.swing.JLabel lblFulllNameAdd;
    private javax.swing.JLabel lblPhoneNumberAdd;
    private javax.swing.JLabel lblPhoneNumberUpdate;
    private javax.swing.JLabel lblPosition;
    private javax.swing.JLabel lblReaderIDAdd;
    private javax.swing.JLabel lblSearchID;
    private javax.swing.JPanel pnAddReader;
    private javax.swing.JPanel pnReader;
    private javax.swing.JPanel pnUpdate;
    private javax.swing.JTabbedPane tabAddEmployee;
    private javax.swing.JTable tbReaderAdd;
    private javax.swing.JTable tbReaderUpdate;
    private javax.swing.JTextField txtAddress;
    private javax.swing.JTextField txtAddressAdd;
    private javax.swing.JTextField txtCareer;
    private javax.swing.JTextField txtCareerAdd;
    private javax.swing.JTextField txtFullName;
    private javax.swing.JTextField txtFullNameAdd;
    private javax.swing.JTextField txtPhoneNumber;
    private javax.swing.JTextField txtPhoneNumberAdd;
    private javax.swing.JTextField txtReaderID;
    private javax.swing.JTextField txtReaderIDAdd;
    private javax.swing.JTextField txtSearchID;
    // End of variables declaration//GEN-END:variables
}
